<template>
  <div>
    <!-- 表格 -->
    <el-table :data="records" border>
      <el-table-column label="序号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="名称" width="150" prop="skuName"></el-table-column>
      <el-table-column label="描述" width="200" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片" width="150">
        <template slot-scope="{row,$index}">
          <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px">
        </template>
      </el-table-column>
      <el-table-column label="重量" width="80" prop="weight"></el-table-column>
      <el-table-column label="价格" width="80" prop="price"></el-table-column>
      <el-table-column label="操作" width="width">
        <template slot-scope="{row,$index}">
          <el-button type="success" icon="el-icon-sort-down" size="mini" v-if="row.isSale==0" @click="sale(row)"></el-button>
          <el-button type="success" icon="el-icon-sort-up" size="mini" v-else @click="cancel(row)"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)"></el-button>
          <el-popconfirm confirm-button-text='确定' cancel-button-text='不用了' :title="`你确定要删除${row.skuName}`" @onConfirm="deleteSku(row)" style="margin-left:10px">
            <el-button type="danger" icon="el-icon-delete" size="mini" slot="reference"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="text-align:center" :total="total" :current-page="page" :page-sizes="[3,5,10]" @current-change="getSkuList" @size-change="handlerSizeChange" :page-size="limit"
      layout="prev,pager,next,jumper,->,sizes,total">
    </el-pagination>

    <!-- 抽屉效 -->
    <el-drawer :visible.sync="drawer" size="50%" :show-close="false">
      <el-row>
        <el-col :span="5">
          名称
        </el-col>
        <el-col :span="16">
          {{skuInfo.skuName}}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          描述
        </el-col>
        <el-col :span="16">
          {{skuInfo.skuDesc}}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          价格
        </el-col>
        <el-col :span="16">
          {{skuInfo.price}}元
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          平台属性
        </el-col>
        <el-col :span="16">
          <template>
            <el-tag type="success" v-for="(attr,index) in skuInfo.skuAttrValueList" :key="attr.id" style="margin-right:10px">{{attr.attrId}}-{{attr.attrName}}</el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          商品图片
        </el-col>
        <el-col :span="16">
          <el-carousel height="350px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="" style="width:100%">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "sku",
  data () {
    return {
      page: 1,
      limit: 10,
      records: [],
      total: 0,
      skuInfo: [],
      drawer: false
    }
  },
  mounted () {
    this.getSkuList()
  },
  methods: {
    async getSkuList (pager = 1) {
      this.page = pager
      let result = await this.$API.sku.reqSkuList(this.page, this.limit)
      if (result.code == 200) {
        this.records = result.data.records
        this.total = result.data.total
      }
    },
    handlerSizeChange (limit) {
      this.limit = limit
      this.getSkuList()
    },
    // 商品上架
    async sale (row) {
      let result = await this.$API.sku.reqSale(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '上架成功' })
        row.isSale = 1
      }
    },
    // 商品下架二点操作
    async cancel (row) {
      let result = await this.$API.sku.reqCanel(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '下架成功' })
        row.isSale = 0
      }
    },
    // 第三个按钮的回调
    edit () {
      this.$message('正在开发中!!!')
    },
    async getSkuInfo (row) {
      // 展示抽屉
      this.drawer = true
      // 获取sku详情的数据
      let result = await this.$API.sku.reqSkuById(row.id)
      if (result.code == 200) {
        this.skuInfo = result.data
      }
    },
    // 删除sku的操作
    async deleteSku (row) {
      let result = await this.$API.sku.reqDeleteSku(row.id)
      console.log(result);
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        this.getSkuList()
      }
    }
  }
}
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px;
}

>>> .el-carousel__button {
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
}
</style>